home *** CD-ROM | disk | FTP | other *** search
- Path: coranto.ucs.mun.ca!usenet
- From: saustin@terra.nlnet.nf.ca (Steve Austin)
- Newsgroups: comp.lang.c++
- Subject: Re: Can you overload a constructor?
- Date: Mon, 18 Mar 1996 00:44:16 GMT
- Organization: Kickham Productions
- Message-ID: <4iibka$6rp@coranto.ucs.mun.ca>
- References: <Do859K.K9B@watserv3.uwaterloo.ca> <31483CDC.4B0C@staff.ichange.com> <4ibqan$br5@si-nic.hrz.uni-siegen.de> <4ic92p$lj6@B1FF.mindspring.com>
- NNTP-Posting-Host: n104h130.nlnet.nf.ca
- X-Newsreader: Forte Agent .99b.112
-
- rudd@mindspring.com (Justin Rudd) rightly points out that constructors
- can be overloaded...but then adds...
-
- >...Of course they do get called but not always.
-
- They certainly do if an instance of the class is being created.
-
- >...Now as for constructors always being called. What if I have this:
- >
- >class Graphic
- >{
- > public:
- > virtual void Draw() = 0;
- >};
- >
- >class Box : public Graphic
- >{
- > public:
- > virtual void Draw();
- >};
- >
- >void main()
- >{
- > Graphic* graph;
- > Box box;
- >
- > graph = &box;
- >
- > graph->Draw();
- >}
- >
- >In this little code segment here I have a POINTER to a Graphic object.
- >It has not been initialized..therefore no constructor call. And then
- >after that...I make the Graphic pointer point to a Box object...so at
- >no time was the Graphic pointer an instance of Graphic.
-
- I'm not sure what point you're making. Constructors are only called
- when an instance of a class is being created. Declaring a pointer to a
- base class obviously is not creating an instance of the class. But
- when you create an instance of Box, the base Graphic constructor will
- be called, followed by the derived Box constructor.
-
- There's no way around it, when you instantiate a class, the
- constructor will always be called. How else would the object be
- created?
-
- - Steve
-
-